/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright (c) Nuvoton Technology Corp. All rights reserved.                                             */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvGPIO.h"
#include "LCD_Driver.h"
#include "Seven_Segment.h"
#include "ScanKey.h"
#include "StopwatchTask.h"

//Library time & Alarm
#include "time_lib.h"

//library RTL RTX
#include <rtl.h>


//--- RTX RTOS
//Task ID 0-9
OS_TID TaskId[10];
//Deklarasi
__task void TaskInitial (void);

#include "DisplayTask.h"
#include "MainMenuTask.h"
#include "SettingTask.h"
#include "AlarmTask.h"

// Display Task
//__task void TaskUpdateLCD (void);
//__task void TaskRunningLED (void);
//__task void TaskScan7Segment (void);

//  Main Menu Task
//__task void TaskMainMenu (void);

//  Edit Task
//__task void TaskBlink(void);
//__task void TaskEditTime(void);

//  Alarm Task
//__task void TaskBlinkAlarm(void);
//__task void TaskEditAlarm(void);


// Initial Task
__task void TaskInitial (void){  

    TaskId[0]=os_tsk_self();
    TaskId[1]=os_tsk_create (TaskUpdateLCD      , 15);
    TaskId[2]=os_tsk_create (TaskRunningLED     , 70);
    TaskId[3]=os_tsk_create (TaskScan7Segment   , 80);
    TaskId[4]=os_tsk_create (TaskMainMenu       , 5);

    os_tsk_delete_self();
}




int main(void)
{   
    xtime _time;
    /* Unlock the protected registers */	
	UNLOCKREG();
   	/* Enable the 12MHz oscillator oscillation */
	DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
    /* Waiting for 12M Xtal stalble */
    SysTimerDelay(1000);
    /* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
	DrvSYS_SelectHCLKSource(0);		
    /*lock the protected registers */
	LOCKREG();
	DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */
   
    // Inisialisasi LCD & Nyalakan backlightnya
    Initial_pannel();  
	clr_all_pannal();
    DrvGPIO_Open(E_GPD, 14, E_IO_OUTPUT);
	DrvGPIO_ClrBit(E_GPD, 14);

    
    //waktu default
    _time.jam    =14;
    _time.menit  =56;
    _time.detik  =59;
    _time.tanggal=23;
    _time.bulan  =4;
    _time.tahun  =12;


    // Aktivasi RTC
    RTCInit(_time);

    // Jalankan scheduler
    os_sys_init(TaskInitial);

	while(1)
	{   
        //tidak akan sampai sini
    }	  		
}



